package com.ibeeking.found.common.utils;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import org.apache.commons.lang3.StringUtils;

/**
 * @author admin * @date 2023年01月10日
 */
public class VersionUtils {

    public static void main(String[] args) {
        String old = "9.9";
        System.out.println(newVersion(old));
        System.out.println(compare(old, "10.1"));
    }

    public static String initVersion() {
        return "1.0";
    }

    public static String newVersion(String oldVersion) {
        if (StringUtils.isEmpty(oldVersion)) {
            return initVersion();
        }
        String[] split = oldVersion.split("\\.");
        int major = Integer.valueOf(split[0]);
        int mini = Integer.valueOf(split[1]);

        if (mini  >= 9) {
            major = major + 1;
            mini = 0;
        } else {
            mini = mini + 1;
        }
        return major + "." + mini;
    }

    public static String newMajorVersion(String oldVersion) {
        if (StringUtils.isEmpty(oldVersion)) {
            return initVersion();
        }
        String[] split = oldVersion.split("\\.");
        int major = Integer.valueOf(split[0]);
        major = major + 1;

        return major + ".0";
    }

    public static boolean compare(String version1, String version2) {
        Assert.isTrue(!StringUtils.isBlank(version1) && !StringUtils.isBlank(version2), "Error: CompareVersion Error: Illegal Argument !", new Object[0]);
        String[] versionArray1 = version1.split("\\.");
        String[] versionArray2 = version2.split("\\.");
        int idx = 0;
        int minLength = Math.min(versionArray1.length, versionArray2.length);

        int diff;
        for(diff = 0; idx < minLength && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0 && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0; ++idx) {
        }

        diff = diff != 0 ? diff : versionArray1.length - versionArray2.length;
        return diff>= 0;
    }
}